package com.itheima.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QuoteException;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
       //链式编程
        Page<CheckItem> page =  PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize())
                .doSelectPage( () ->checkItemDao.findPage(queryPageBean.getQueryString()));
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id查询该检查项的具体数据
    @Override
    public Result findById(Integer id) {

        //如果没查到数据,指定异常
        try {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemDao.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    //编辑检查项数据  checkItem
    @Override
    public Result edit(CheckItem checkItem) {
        try {
            if (checkItem==null||StringUtils.isBlank(checkItem.getCode()) || StringUtils.isBlank(checkItem.getName())) {
              return new Result( false,"检查项编号和名称不能为空");
            }
            checkItemDao.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    //添加检查项
    @Override
    public Result add(CheckItem checkItem) {

        try {
            if (checkItem == null || StringUtils.isBlank(checkItem.getCode()) || StringUtils.isBlank(checkItem.getName())) {
                //return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
                throw new QuoteException("检查项名称和编号不能为空,请重新输入");
            }
            checkItemDao.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (QuoteException e) {
            return new Result(false, "检查项名称和编号不能为空");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    //查询所有检查项数据
    @Override
    public Result findAll() {

        try {
            List<CheckItem> checkItemList = checkItemDao.findAll();
            if (CollectionUtils.isEmpty(checkItemList)) {
                return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
            }
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    //删除检查项
    @Override
    public Result delete(Integer id) {

        // 查询当前检查项是否和检查组关联

        try {
            long count = checkItemDao.findCountByCheckItemId(id);
            if (count > 0) {
                return new Result(false, "删除失败!该检查项已被检查组使用!");
            }
            checkItemDao.deleteById(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);

    }

}